﻿
/** Pete's Tools package */
package pete.tools
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	/**
	 * @version
	 * 0.1
	 *
	 * @description 
	 * Creates a copy of a target DisplayObject, and splits into multiple color channels. 
	 * 
	 * @example <pre>
	 * var test:Sprite = new Sprite ();
	 * var split:SplitChannels = new SplitChannels (test);
	 * split.red.x += 10;
	 * split.green.y -= 20;
	 * addChild (split); </pre>
	 */
	public class SplitChannels extends Sprite
	{
		/**
		 * Accesses the Red Channel.
		 * @see green
		 * @see blue
		 */
		public  var red:Bitmap;
		
		/**
		 * Accesses the Green Channel.
		 * @see red
		 * @see blue
		 */
		public  var green:Bitmap;
		
		/**
		 * Accesses the Blue Channel.
		 * @see red
		 * @see green
		 */
		public  var blue:Bitmap;
		
		private var _target:*;
		
		
		/**
		 * Creates a new instance of the SplitChannels Object.
		 * @param target The targetDisplayObject to split into multiple color channels.
		 */
		public function SplitChannels (target:Object)
		{
			_target = target;
			drawChannels ();
		}
		
		
		private function drawChannels ():void
		{
			var targetBMD:BitmapData 	= new BitmapData (_target.width, _target.height, true, 0);
			var matrix:Matrix		= new Matrix ();
			matrix.scale			(_target.scaleX, _target.scaleY);
			targetBMD.draw		(_target, matrix);
			
			var targetRect:Rectangle	= new Rectangle (0, 0, _target.width, _target.height);
			
			var redBMD:BitmapData	= new BitmapData (_target.width, _target.height, true );
			redBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.GREEN, BitmapDataChannel.GREEN);
			redBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.BLUE, BitmapDataChannel.BLUE);
			redBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
			
			var greenBMD:BitmapData	= new BitmapData (_target.width, _target.height, true );
			greenBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.RED, BitmapDataChannel.RED);
			greenBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.BLUE, BitmapDataChannel.BLUE);
			greenBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
			
			var blueBMD:BitmapData	= new BitmapData (_target.width, _target.height, true );
			blueBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.RED, BitmapDataChannel.RED);
			blueBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.GREEN, BitmapDataChannel.GREEN);
			blueBMD.copyChannel		(targetBMD, targetRect, new Point (), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
			
			red 				= createChannel (redBMD);
			green				= createChannel (greenBMD);
			blue				= createChannel (blueBMD);
			
			this.blendMode		= BlendMode.LAYER;
		}
		
		
		private function createChannel ($bmd:BitmapData):Bitmap
		{
			var channel:Bitmap		= new Bitmap ($bmd);
			channel.blendMode		= BlendMode.DARKEN;
			
			addChild			(channel);
			return				(channel);
		}
		
	}
	
}